<?

//se colocado no fim, faz auto submit
//<script type="text/javascript">
//document.forms[0].submit();
//</script> */


session_start();
// $DisplayStandardValuesInForm
// If set to true  : Standard values for Gateway,GatewayUser and Gateway Password will be shown in form
//
$DisplayStandardValuesInForm =true;
//
// $UseStandardValues
// If set to true : Standard values will be used to send SMS and Gateway,User,Password will not be shown in form
//
$UseStandardValues = false;
//
// $StandardGateway
// IP-address of Gateway
//
$StandardGateway ="192.168.0.100";//precisa do ip real
//
// $StandardUser
// Username for gateway login
//
$StandardUser = $_SESSION['tel'];//"voip";
//
// $StandardPassword
// Password for gateway login
//
$StandardPassword = $_SESSION['pin'];//"1234";
//
//*********************************************************************************
// END OF CONFIGURATION
//*********************************************************************************
if($UseStandardValues)
{
$send_gateway = $StandardGateway;
$send_user = $StandardUser;
$send_pass = $StandardPassword;
}else{
$send_gateway =$_POST['gateway'];
$send_user = $_POST['user'];
$send_pass = $_POST['password'];
}
$send_to = $_POST['mobile'];
$send_msg = $_POST['message'];


$GotGateway = ($send_gateway != "");
$GotUser = ($send_user != "");
$GotPass = ($send_pass != "");
$GotMobile = ($send_to != "");
$GotMessage = ($send_msg != "");
$GotSendInfo = ($GotGateway && $GotUser && $GotPass && $GotMobile && $GotMessage);


?>
<form action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
<center>
<?


if($GotSendInfo){
$msgnbr=send_sms($send_to,$send_msg,$send_gateway,$send_user,$send_pass);
if($msgnbr!=""){
echo "SMS Send Sucess<BR>";
echo "Message Sent to : ".$send_to." ID : ".$msgnbr."<BR><HR><BR>";
}else{
echo "SMS Send Error<BR><HR><BR>";
}
}



if($UseStandardValues)
{
messagefields($send_to,$send_msg);
}else{
if($DisplayStandardValuesInForm){
gatewayfields($StandardGateway,$StandardUser,$StandardPassword);
}else{
gatewayfields();
}
messagefields($send_to,$send_msg);
}

?>

</center>
</form>
<?


function gatewayfields($fhost="",$fuser="",$fpass=""){
echo "<table border=\"2\" cellpadding=\"2\" cellspacing=\"2\">\n";
echo "<tr><td colspan=\"2\"><B>Gateway</B></td></tr>\n";
echo "<tr><td>Gateway IP:</td><td><input type=\"text\" name=\"gateway\" size=\"16\" value=\"$fhost\" /></td></tr>\n";
echo "<tr><td>User :</td><td><input type=\"text\" name=\"user\" size=\"16\" value=\"$fuser\" /></td></tr>\n";
echo "<tr><td>Password :</td><td><input type=\"password\" name=\"password\" size=\"16\" value=\"$fpass\" /></td></tr>\n";

echo "</table>\n";
echo "<BR><HR width=\"300\"><BR>\n";
}

function messagefields($fto="",$fmsg=""){
echo "<table border=\"2\" cellpadding=\"2\" cellspacing=\"2\">\n";
echo "<tr><td colspan=\"2\"><B>SMS</B></td></tr>\n";
echo "<tr><td>Mobile Number : <BR><i> ex. +4512345678 or 12345678</i></td>";
echo "<td ALIGN=\"CENTER\"><input type=\"text\" name=\"mobile\" size=\"16\" value=\"$fto\" style=\"background-color: #0066ff;\"/></td></tr>\n";
echo "<tr><td>Message :</td><td><textarea rows=\"10\" cols=\"16\" name=\"message\" style=\"background-color: #0066ff;\">$fmsg</textarea></td></tr>\n";
echo "<tr><td colspan=\"2\"><input type=\"submit\" value=\"Send SMS\" /></td></tr>\n";
echo "</table>\n";
}


function send_sms($tomobile,$xbody,$xhost,$xusername,$xpassword)
{
$myoutput = "";
$fp = fsockopen("$xhost", 23, $errno, $errstr, 30);
if (!$fp) 
{
echo "$errstr ($errno)<br />\n";
die;
}
sleep(2);

$cmd = "$xusername\r";
fputs($fp, $cmd, strlen($cmd));
sleep(1);

$cmd = "$xpassword\r";
fputs($fp, $cmd, strlen($cmd));
sleep(1);

$cmd = "module\r";
fputs($fp, $cmd, strlen($cmd));
sleep(2);

$cmd = "ate1\r";
fputs($fp, $cmd, strlen($cmd));
sleep(1);
	
$cmd = "AT+CSCS=\"GSM\"\r";
fputs($fp, $cmd, strlen($cmd));
sleep(2);	

//Select SMS Message Format... (0=PDU Mode, 1=Text Mode)
$cmd = "at+cmgf=1\r";
fputs($fp, $cmd, strlen($cmd));
$myoutput .= fread($fp, 256);
sleep(2);

//Send SMS Message...
$cmd = "at+cmgs=\"$tomobile\"\r";
fputs($fp, $cmd, strlen($cmd));
sleep(2);
$myoutput .= fread($fp, 256);

//Body...
$cmd = "$xbody\r\x1a"; //Ctrl-Z
fputs($fp, $cmd, strlen($cmd));
$res = " ";
$myoutput = "";
  
stream_set_timeout($fp, 5); //5 seconds read timeout


while ($res != "")
  { 
    $res = fread($fp, 256);
    $myoutput .= $res;
  }


$tmpsms_number = explode('+CMGS: ', $myoutput);
$sms_number = explode(' ', $tmpsms_number[1]);
$actnum = $sms_number[0];
$actnum = str_replace(" ","",$actnum);
$actnum = str_replace("\r","",$actnum);
$actnum = str_replace("\n","",$actnum);
$actnum = str_replace("\t","",$actnum);
$actlen= strlen($actnum)-1;
$actnum = substr($actnum,0,$actlen);
    fclose($fp);
return $actnum;		
  }
	
?>


